﻿namespace CommonData.Entity
{
    using System;
    using System.Collections.Generic;

    public static class EntityTypeCache
    {
        private static IDictionary<Type, TableInfo> cache = null;

        static EntityTypeCache()
        {
            cache = new Dictionary<Type, TableInfo>();
        }

        public static TableInfo GetTableInfo<T>() where T: IEntity
        {
            Type type = typeof(T);
            return GetTableInfo(type);
        }

        public static TableInfo GetTableInfo(IEntity entity)
        {
            return GetTableInfo(entity.GetType());
        }

        public static TableInfo GetTableInfo(Type type)
        {
            if (cache.ContainsKey(type))
            {
                return cache[type];
            }
            return null;
        }

        public static void InsertTableInfo(IEntity entity, TableInfo tableInfo)
        {
            InsertTableInfo(entity.GetType(), tableInfo);
        }

        public static void InsertTableInfo(Type type, TableInfo tableInfo)
        {
            if (!cache.ContainsKey(type))
            {
                cache.Add(type, tableInfo);
            }
        }
    }
}

